package cn.com.enersun.iec61850.web.mapper;

import cn.com.enersun.iec61850.web.domain.ServerModelValue;
import org.apache.ibatis.annotations.*;
import org.openmuc.openiec61850.Fc;

/**
 * Created by YYC on 2017/5/4.
 */
@Mapper
public interface ServerModelValueMapper {
    //指定类属性与数据库字段对应关系
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "date", column = "datetime"),
            @Result(property = "deviceName", column = "deviceName"),
            @Result(property = "selfNode", column = "selfNode"),
            @Result(property = "nodeValue", column = "nodeValue"),
            @Result(property = "nodeFc", column = "nodeFc")
    })

    @Select("SELECT id, datetime, deviceName, selfNode, nodeValue, nodeFc from modelvalues WHERE deviceName = #{deviceName} AND selfNode = #{selfNode} AND nodeFc = #{nodeFc}")
    ServerModelValue selectServerModelValue(String deviceName, String selfNode, Fc fc);

    @Insert("INSERT into modelvalues(id, datetime, deviceName, selfNode, nodeValue) VALUES(#{id}, #{date}, #{deviceName}, #{selfNode}, #{nodeValue})")
    //在执行INSERT语句之前给id属性自动生成UUID值
    @SelectKey(statement="select replace(uuid(),'-','')", keyProperty="id", resultType=String.class, before=true)
    void insertServerModelValue(ServerModelValue serverModelValue);

    @Update("UPDATE modelvalues SET nodeValue=#{nodeValue} WHERE deviceName=#{deviceName} AND selfNode =#{selfNode}")
    void updateServerModelValue(ServerModelValue serverModelValue);

    @Delete("DELETE FROM modelvalues WHERE deviceName = #{deviceName} AND selfNode = #{selfNode} AND nodeFc = #{nodeFc}")
    void deleteServerModelValue(String deviceName, String selfNode, Fc fc);
}
